package site.luojie.flowabledemo;

import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    public static void main(String[] args) {

        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://luojie.site:33306/flowable?characterEncoding=UTF-8")
                .setJdbcUsername("root")
                .setJdbcPassword("123456")
                .setJdbcDriver("com.mysql.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        ProcessEngine processEngine = cfg.buildProcessEngine();

        // 流程定义部署至Flowable引擎
//        RepositoryService repositoryService = processEngine.getRepositoryService();
//        Deployment deployment = repositoryService.createDeployment()
//                .addClasspathResource("processes/ContractProcess.bpmn20.xml")
//                .deploy();
//
//        // 通过API查询验证流程定义已经部署在引擎中
//        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
//                .deploymentId(deployment.getId())
//                .singleResult();
//        System.out.println("Found process definition : " + processDefinition.getName());
//
//        // 启动一个流程实例
        HashMap<String, Object> map = new HashMap<>();
        map.put("type", 2);
        map.put("contractUser", "user_1");
        RuntimeService runtimeService = processEngine.getRuntimeService();
        runtimeService.startProcessInstanceByKey("contractProcess", map);

        TaskService taskService = processEngine.getTaskService();
        List<Task> managers = taskService.createTaskQuery().taskCandidateGroup("managers").list();

        List<Task> users = taskService.createTaskQuery().taskAssignee("user_1").list();
        managers.forEach(manage -> System.out.println(manage.getName()));

        users.forEach(user -> {
            System.out.println(user.getName());
            taskService.complete(user.getId(), new HashMap<String, Object>() {{
                this.put("approved", true);
            }});
        });
        System.out.println();


    }

}
